package com.witdata.rabbit.display;

import com.alibaba.fastjson2.JSON;
import com.witdata.common.domain.dto.InstanceDto;
import com.witdata.rabbit.domain.Handle;
import com.witdata.rabbit.producer.InstanceProducer;
import com.witdata.rabbit.service.InstanceService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Data
@RequiredArgsConstructor
@Component
@Slf4j
public class InstanceDisplay {
    private final InstanceService instanceService;
    private final InstanceProducer instanceProducer;

    public void registerInstance(String data) {
        InstanceDto instanceDto = JSON.parseObject(data, InstanceDto.class);
        InstanceDto registeredInstance = instanceService.registerInstance(instanceDto);
        instanceProducer.syncSend(0L, Handle.RegisterInstance, registeredInstance.getId());
    }

    public void heartbeat(String data) {
        InstanceDto instanceDto = JSON.parseObject(data, InstanceDto.class);
        log.debug("{}心跳监测成功！", instanceDto);
        instanceService.updateInstance(instanceDto);
    }


}
